package com.zj.core.adapter

import android.content.Context
import android.support.constraint.ConstraintLayout
import android.support.v7.widget.RecyclerView
import android.view.*
import android.widget.CheckBox
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.fileutils.FontUtils
import com.zj.core.R
import com.zj.core.bean.VoiceTemplateBean
import com.zj.core.tools.Constant
import com.zj.core.ui.ItemListTemplateUi
import com.zj.core.view.MeuseTextView
import org.jetbrains.anko.*

/**
 * Created by Administrator on 2019/5/6.
 */

class TemplateListAdapter(context: Context, detailList: ArrayList<VoiceTemplateBean.VoiceWordBean.DatEntity.RowsEntity>?, type:Int) : RecyclerView.Adapter<TemplateListAdapter.RecordOcrAdapterHolder>() {
    private var mContext:Context?=context
    private var mDetailList:ArrayList<VoiceTemplateBean.VoiceWordBean.DatEntity.RowsEntity>?=detailList
    private var mAllSelect = false
    private var mDialogType=type

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecordOcrAdapterHolder {
        return RecordOcrAdapterHolder(ItemListTemplateUi().createView(AnkoContext.create(parent.context, parent)))
    }

    override fun onBindViewHolder(holder: RecordOcrAdapterHolder, position: Int) {
        with(holder){
            if (position == 0) {
                this.recordItemLine?.visibility = View.GONE
            } else {
                this.recordItemLine?.visibility = View.VISIBLE
            }

            if (mDialogType == 0) {                                   //模板
                this.serialItemNumTv?.visibility = View.GONE
            } else if(mDialogType==1){                                //内容项
                this.serialItemNumTv?.visibility = View.VISIBLE
                this.serialItemNumTv?.text = mDetailList?.get(position)?.wouldid
            }

            this.templateStrTv?.text = mDetailList?.get(position)?.wouldword
            this.checkBoxView?.isChecked = mAllSelect
            this.pushImgView?.setOnClickListener {
                //展开
                detailShowView(this, position)
            }

            this.templateStrTv?.setOnClickListener { detailShowView(this, position) }
            val fontWidth=FontUtils.meauseWidthForText(mContext,mDetailList?.get(position)?.wouldword)

            if (fontWidth> Constant.meuseTextViewWidth){
                this.templateStrTv?.post {
                    this.pushImgView?.visibility = View.VISIBLE
                }
            } else {
                this.templateStrTv?.post {
                    this.pushImgView?.visibility = View.INVISIBLE
                }
            }
        }
    }

    /**
     * 展开折叠
     *
     * @param holder
     * @param position
     */
    private fun detailShowView(holder: RecordOcrAdapterHolder, position: Int) {
        with(holder){
            if (pushImgView?.tag == null || pushImgView?.tag == "0") {
                pushImgView?.setBackgroundResource(R.mipmap.simple_img)
                pushImgView?.tag = "1"

                templateStrTv?.maxLines = 99
                templateStrTv?.text = mDetailList?.get(position)?.wouldword
            } else {                                                         //折叠
                pushImgView?.setBackgroundResource(R.mipmap.detail_img)
                pushImgView?.tag = "0"

                templateStrTv?.maxLines = 1
                templateStrTv?.text = mDetailList?.get(position)?.wouldword
            }
        }
    }

    override fun getItemCount(): Int {
        return mDetailList!!.size
    }

    /**
     * 全选状态
     */
    fun setAllCheck(allCheck: Boolean) {
        this.mAllSelect = allCheck
    }

    inner class RecordOcrAdapterHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var checkBoxView: CheckBox? =itemView.find(R.id.check_box_view)
        var templateStrTv: MeuseTextView? =itemView.find(R.id.template_str_tv)
        var pushImgView: ImageView? =itemView.find(R.id.push_img_view)
        var recordItemLine: View? =itemView.find(R.id.record_item_line)
        var serialItemNumTv:TextView?=itemView.find(R.id.item_serial_number)
    }
}